package com.foodplus.www.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.foodplus.www.adapter.shop.GoodAdapter;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.good.GoodsListBean;
import com.foodplus.www.bean.shop.ShopTagBean;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.ShopTagPresenter;
import com.foodplus.www.view.impl.ShopTagView;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TagActivity extends AppCompatActivity implements ShopTagView {

    @BindView(R.id.tag_title_tv)
    TextView tagTitleTv;
    @BindView(R.id.tag_back)
    ImageView tagBack;
    @BindView(R.id.tag_recycler)
    LRecyclerView tagRecycler;

    private String id;
    private String titleStr;

    //适配器
    private GoodAdapter adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    //容器
    private List<GoodsListBean> list;

    //p层数据源
    private ShopTagPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tag);
        ButterKnife.bind(this);
        list = new ArrayList<>();

        //获取item的special_id
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        titleStr = intent.getStringExtra("title");

        initView();

        //获取数据
        getData();
    }

    private void initView() {
        //RecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        tagRecycler.setLayoutManager(manager);
        adapter = new GoodAdapter(list, this);
        setRecycleItemClick();
        //加入query
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        tagRecycler.setPullRefreshEnabled(false);
        tagRecycler.setAdapter(lRecyclerViewAdapter);
    }

    private void setRecycleItemClick() {
        adapter.setOnItemClickListener(new GoodAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(int position) {
                Intent intent = new Intent(TagActivity.this, SipGoodActivity.class);
                intent.putExtra("id", list.get(position-1).getGoods_id());
                TagActivity.this.startActivity(intent);
                TagActivity.this.overridePendingTransition(R.anim.shop_trans_enter, 0);
            }
        });
    }

    @OnClick(R.id.tag_back)
    public void onClick(View v) {
        getBack();
    }


    private void getData() {
        presenter = new ShopTagPresenter(this);
        presenter.getShopTagData(ParamManager.getInstance().getTagParams(id));
    }


    @Override
    public void onSuccess(ShopTagBean shopTagBean) {
        list.clear();
        //整理数据
        list.addAll(shopTagBean.getDatas().getGoods_list());
        //载入标题
        tagTitleTv.setText(titleStr);
        //载入recycler主体的数据
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    private void getBack() {
        Log.e("Good", "back");
        TagActivity.this.overridePendingTransition(0, R.anim.shop_trans_out);
        TagActivity.this.finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        getBack();
    }
}
